__author_ = 'Gary Zhang'

"Two Sum"

import time


def fun1(num, target):
    start_time = time.time()
    # num = [3, 3]
    # target = 6
    output = []
    for i in range(len(num)):
        for j in range(i + 1, len(num)):
            if num[i] + num[j] == target:
                output.append(i)
                output.append(j)
                print(output)
                output = []
            else:
                continue
    end_time = time.time()
    cost_time = end_time-start_time
    print('time cost : %.7f sec' %cost_time)


def fun2(num, target):
    output = []
    for i in range(len(num)):
        x = target - num[i]
        if x in num:
            x_index = num.index(x)
            if i == x_index:
                continue
            else:
                print(i, x_index)
                break
        else:
            continue


def fun3(num, target):
    """
    :type num: List[int]
    :type target: int
    :rtype: List[int]
    """
    # 用len()方法取得nums列表长度
    n = len(num)
    # 创建一个空字典
    d = {}
    for x in range(n):
        a = target - num[x]
        # 字典d中存在nums[x]时
        if num[x] in d:
            print(d[num[x]], x)
            return d[num[x]], x
        # 否则往字典增加键/值对
        else:
            d[a] = x
    # 边往字典增加键/值对，边与nums[x]进行对比


if __name__ == '__main__':
    fun1([3, 3], 6)
    fun2([3, 3], 6)
    fun2([1, 2, 3, 4, 5], 6)
    fun3([1, 2, 3, 4, 5], 6)
